﻿using ShiNiu.ServiceStackRedis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ShiNiu.RedisDemo
{
    class Program
    {
        static bool done;
        static object locker = new object();
        static void Main(string[] args)
        {
            RedisService RedisService = new RedisService();

            //RedisService.Set<string>("cccc", "11111");
            //string dtr = RedisService.Get<string>("cccc"); 
            //Console.WriteLine(dtr);

            Thread oGetArgThread = new Thread(new ThreadStart(Test));
            oGetArgThread.Start();
            Thread oGetArgThread2 = new Thread(new ThreadStart(Test));
            oGetArgThread2.Start();
            //Thread oGetArgThread3 = new Thread(new ThreadStart(Test2));
            //oGetArgThread3.Start();

            //Thread oGetArgThread4 = new Thread(new ThreadStart(Test2));
            //oGetArgThread4.Start();

            #region Hash

            UserInfo info = new UserInfo();
            info.Age = "666";
            info.Sex = "66";
            info.Name = "6666666";
            info.id = "66";
            RedisService.StoreAsHash(info);
            //Console.WriteLine("请输入用户名：");
            //string username = Console.ReadLine();
            //Console.WriteLine("请输入密码：");
            //string password = Console.ReadLine();
            //RedisService.SetEntryInHash("userinfo", "username1", info);
            //RedisService.SetEntryInHash("userinfo", "password1", password);
            //List<string> keylist = RedisService.GetHashKeys("userinfo");
            //List<string> valuelist = RedisService.GetHashValues("userinfo");
            //for (int i = 0; i < keylist.Count; i++)
            //{
            //    Console.WriteLine("key为：{0},value为：{1}", keylist[i], valuelist[i]);
            //}
            #endregion

            #region
            //RedisService.AddItemToSet("a", "aaaa1");
            //RedisService.AddItemToSet("a", "aaaa2");
            //RedisService.AddItemToSet("a", "aaaa3");
            //Console.WriteLine("输出Set a：");
            //System.Collections.Generic.HashSet<string> hashset = RedisService.GetAllItemsFromSet("a");
            //foreach (string str in hashset)
            //{
            //    Console.WriteLine(str);
            //}
            #endregion


            //Test();
            Console.ReadKey();

        }

        static void Test()
        {
            lock (locker)
            {
                if (!done)
                {
                    for (int i = 0; i < 1000; i++)
                    {
                        RedisService RedisService = new RedisService();
                        //SnRedisBase redis = new SnRedisBase();
                        //redis.Set<string>("admin" + i.ToString(), i.ToString());
                        //string dtr = redis.Get<string>("admin" + i.ToString());

                        // SnRedisBase.Core.Add<string>("admin"+i.ToString(), i.ToString());
                        // string dtr = SnRedisBase.Core.Get<string>("admin" + i.ToString());
                        //SnRedisBase.Core.Remove("admin" + i.ToString());
                        // SnRedisBase.RedisCore.StoreAll<List<string>>(null); 

                        //SnRedisBase.ClientRedis.Add<string>("1admin" + i.ToString(), i.ToString());
                        // string dtr = SnRedisBase.ClientRedis.Get<string>("1admin" + i.ToString());


                       // RedisService.Set<string>("1admin" + i.ToString(), i.ToString());
                        string dtr = RedisService.Get<string>("1admin" + i.ToString());
                       RedisService.Remove("1admin" + i.ToString());
                        Console.WriteLine(dtr);

                        //SnRedisBase.ClientRedis.Remove("1admin" + i.ToString());
                        //Console.WriteLine("1成功" + i.ToString());
                        //SnRedisBase.Core.Remove("admin" + i.ToString());

                        //Thread.Sleep(1000);
                        done = true;
                    }                      
                }
            }
        }

        static void Test2()
        {
            var List = RedisInfo.GetList();
            #region List链表缓存
            //RedisService.AddList<UserInfo>("RedisListTable", List);//List链表缓存 
            #endregion

            for (int i = 0; i < 1000; i++)
            {
                #region Set集合缓存-元素唯一不可重复
                ////RedisService.Set_Add<List<UserInfo>>("RedisSet" + i.ToString(), List);//Set集合缓存
                //RedisService.Set_Add<List<UserInfo>>("RedisSet", List);
                #endregion

                #region Hash缓存
                RedisService.Hash_Set<List<UserInfo>>("RedisHash", i.ToString(), List);//Hash缓存 
                #endregion

                //SnRedisBase redis = new SnRedisBase();
                //redis.Set<string>("admin" + i.ToString(), i.ToString());
                //string dtr = redis.Get<string>("admin" + i.ToString());

                // SnRedisBase.Core.Add<string>("admin"+i.ToString(), i.ToString());
                // string dtr = SnRedisBase.Core.Get<string>("admin" + i.ToString());
                //SnRedisBase.Core.Remove("admin" + i.ToString());
                // SnRedisBase.RedisCore.StoreAll<List<string>>(null); 

                //SnRedisBase.ClientRedis.Add<string>("2admin" + i.ToString(), i.ToString());
                //string dtr = SnRedisBase.ClientRedis.Get<string>("2admin" + i.ToString());

                //RedisService.Set<string>("2admin" + i.ToString(), i.ToString());
                //string dtr = RedisService.Get<string>("2admin" + i.ToString());
                //Console.WriteLine(dtr);
                //var list = RedisService.GetList<UserInfo>("RedisListTable").ToList();

                #region List链表缓存-OK
                //var list = RedisService.List_GetList<UserInfo>("RedisListTable").ToList();
                //list.ForEach(s => Console.WriteLine("2成功 :" + "年龄：" + s.Age + "|" + i.ToString()));
                #endregion

                #region Set集合缓存-OK
                ////var list = RedisService.GetRandomItemFromSets<List<UserInfo>>("RedisSet" + i.ToString());
                //var list = RedisService.GetRandomItemFromSets<List<UserInfo>>("RedisSet");
                //list.ForEach(s => Console.WriteLine("2成功 :" + "年龄：" + s.Age + "|" + i.ToString()));
                ////RedisService.Set_Remove<List<UserInfo>>("RedisSet" + i.ToString());
                #endregion

                #region Hash缓存-OK
                var list = RedisService.Hash_Get<List<UserInfo>>("RedisHash", i.ToString()).ToList();
                list.ForEach(s => Console.WriteLine("2成功 :" + "年龄：" + s.Age + "|" + i.ToString()));
                #endregion

                //Console.WriteLine("2成功" + i.ToString());
                //RedisService.Remove("2admin" + i.ToString());
                //SnRedisBase.Core.Remove("admin" + i.ToString());
                //Console.WriteLine("2成功" + i.ToString());
                //Thread.Sleep(1000);
            }
        }
    }
}
